package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.InviteRecordAdapter;
import com.yiwei.zhh.adapter.InviteRecordV2Adapter;
import com.yiwei.zhh.api.callback.InviteRecordContract;
import com.yiwei.zhh.api.requestInfo.InviteRecordRequest;
import com.yiwei.zhh.model.MoneyRecordModel;
import com.yiwei.zhh.model.MoneyRewardModel;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.model.ShareRecordModel;
import com.yiwei.zhh.util.OnLoadeMoreListener;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.Dialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.sharesdk.onekeyshare.themes.OnekeyShare;

/**
 * Created by xsooy-pc on 2017/6/6.
 */

public class InviteRecordActivity extends BaseActivity implements InviteRecordContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    @BindView(R.id.txt_one)
    TextView txtOne;
    @BindView(R.id.txt_two)
    TextView txtTwo;
    @BindView(R.id.txt_three)
    TextView txtThree;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {

            api.getShareRecord(page, 15);
        }
    };
    private Unbinder unbiner;
    private InviteRecordRequest api;
    private InviteRecordV2Adapter adapter;
    private String title;
    private String content;
    private String url;
    private String imageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_record);
        unbiner = ButterKnife.bind(this);
        initTouMing();
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        content = intent.getStringExtra("content");
        url = intent.getStringExtra("url");
        imageUrl = intent.getStringExtra("imageUrl");
        api = InviteRecordRequest.create();
        api.attachView(this);
        refresh.setOnRefreshListener(this);
        adapter = new InviteRecordV2Adapter(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        api.getShareRecord(1, 15);
    }

    private void showShare() {
        if (TextUtils.isEmpty(title)) {
            return;
        }
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(content);
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(imageUrl);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment(content);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(title);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

// 启动分享GUI
        oks.show(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        unbiner.unbind();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
    }


    @Override
    public void onRefresh() {
        onLoadeMoreListener.setPage(1, 0, false);
        api.getShareRecord(1, 15);
    }


    @OnClick({R.id.btn_back, R.id.btn_invite})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_invite:
                showShare();
                break;

        }
    }

    @Override
    public void getShareRecordSuccess(int page, MoneyRecordModel item) {
        MoneyRecordModel.DataBean.StatisticsBean shareMoney = item.data.statistics;
        txtMoney.setText(shareMoney.total + "");
        UI.setTvHtml(txtOne, R.string.invite_v2_record, "注册成功", shareMoney.register + "");
        UI.setTvHtml(txtTwo, R.string.invite_v2_record, "借款成功", shareMoney.borrow + "");
        UI.setTvHtml(txtThree, R.string.invite_v2_record, "还款成功", shareMoney.repay + "");
        txtInfo.setText("已邀请" + shareMoney.register + "人");
//        txtOne.setText("成功注册\n" + shareMoney.register + "人");
//        txtTwo.setText("借款成功\n" + shareMoney.borrow + "人");
//        txtThree.setText("还款成功\n" + shareMoney.repay + "人");
            adapter.add(item.data.lists, page == 1);
        if (adapter.getmSource().size() == 0 && item.data.lists.size() <= 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
        } else {
            layoutEmpty.setVisibility(View.GONE);
        }
    }


}



